package com.zhengtoudai.http;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.zhengtoudai.base.App;
import com.zhengtoudai.ui.activity.LoginActivity;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;

import retrofit2.adapter.rxjava.HttpException;



/**
 * Created by Administrator on 2016/8/25 0025.
 */

public class HttpUtil {

    //处理请求失败问题
    public static void disposeOnFailure(Context context,Throwable e){
        Log.e("Throwable",e.getMessage());
        if (context==null){
            return;
        }
        if (!Tool.isNetworkAvailable(context)){
            AbToastUtil.showToast(context,"网络不可用");
            return;
        }
        if (e instanceof HttpException){
            HttpException exception= (HttpException) e;
            if (exception.code()==401){
                //清空ssionid
                App.mApp.logout();
                context.startActivity(new Intent(context,LoginActivity.class));
                AbToastUtil.showToast(context,"请先登录");

            }else if (exception.code()==500){
                AbToastUtil.showToast(context,"服务器异常，请稍后重试");
            }else{
               Log.e("onError",exception.getMessage());
            }
        }
    }

    //处理请求失败问题
    public static String getFailure(Context context,Throwable e){
        Log.e("Throwable",e.getMessage());
        if (context==null){
            return null;
        }
        if (!Tool.isNetworkAvailable(context)){
            return "网络不可用";
        }
        if (e instanceof HttpException){
            HttpException exception= (HttpException) e;
            if (exception.code()==401){
                //清空ssionid
                App.mApp.logout();
                context.startActivity(new Intent(context,LoginActivity.class));
                AbToastUtil.showToast(context,"请先登录");

            }else if (exception.code()==500){
                return "服务器异常，请稍后重试";
            }else{
                Log.e("onError",exception.getMessage());
            }
        }
        return null;
    }
}
